<?php
class Emp_Room extends Public_Controller{
	function __construct(){
		parent::__construct();
		$this->template
			->set_layout('salary_toobar.html');
		$this->load->model('room_m');
		menu_active();
		//print_r($this->current_profile);
	}
	
	public function map(){
		if($this->role_full){
			//echo $this->uri->uri_string();
			$this->session->set_userdata('redirect', $this->uri->uri_string());
			//echo $this->session->flashdata('redirect');
			$this->template->set('admin',true)->append_css('toolbar.css');
			$this->action = 'new|'.site_url('admin/emp_room/create').'|'.lang('global:addnew');
			$this->action .= '+new|'.site_url('users/register').'|'.lang('users:register_user');
			$this->action .= '+excel|'.site_url('emp_room/export_employ').'|'.lang('excel:export_employ');
		}
		else{
			$this->template->set('admin',true)->append_css('toolbar.css');
			$this->action = 'excel|'.site_url('emp_room/export_employ').'|'.lang('excel:export_employ');
		}
		//exit;
		$link = 'emp_room/view/';
		$rooms = $this->room_m->get_all_list(0,NULL,NULL,$link);
		$this->template
			->set('rooms',$rooms)
			->set('action',$this->action)
			->append_css('map.css')
			->title(lang('room:map_company'))
			->build($this->controller.'/emp_room/'.$this->method);
	}
	
	public function view($id){
		$this->load->model('emp_profile/profile_m');
		$this->load->model('users/list_m');
		$room = $this->room_m->get($id);
		//lay toan bo nhan vien thuoc cap hien tai + cap con
		$this->db->where('parent_id',$id);
		$childs = $this->room_m->get_all();
		$room_id_childs = array();
		foreach($childs as $i){
			$room_id_childs[] = $i->id;
		}
		$tmp = array();
		$this->room_m->array_room_child($id,$tmp);
		$list_id_room_childs = join(',',$tmp); // 4,2,1
		//echo $list_id_room_childs;exit;
		$employees = $this->profile_m->get_by_room($id,$list_id_room_childs);
		$male = 0;
		$female = 0;
		$literacy_select = $this->list_m->literacy_select;
		$literacy = array();
		
		$color = array('#0076A0','#00A02A','#ccc','#000','#F38630');
		foreach($literacy_select as $key=>$value)
			$literacy[] = array('id'=>$key,'title'=>$value,'value'=>0);
		for($i=0;$i<count($literacy);$i++){
			$literacy[$i]['background'] = $color[$i];
		}
		foreach($employees as $item){
			if($item->sex == 0 )
				$female += 1;
			else
				$male += 1;
			$id_literacy = $item->literacy;
			foreach($literacy as &$k){
				if($k['id']==$id_literacy){
					$k['value'] += 1;
				}
			}
		}
		//CHART
		$json_literacy = array();
		foreach($literacy as $item)
			$json_literacy[] = array('value'=>$item['value'],'color'=>$item['background']);
		$json_literacy = json_encode($json_literacy);
		$sex_select = $this->list_m->sex_select;
		$this->load->model('users/groups_m');
		$param['not_name'] = array('admin');
		$groups = $this->groups_m->get_all($param);
		$groups_select = array_for_select($groups,'id','description');
		//list room
		//$this->load->model('emp_room/room_m');
		$link = 'emp_room/view/';
		$rooms = $this->room_m->get_all_list($id,NULL,NULL,$link);
		$this->template
			->title(lang('room:map').$room->title)
			->set('room',$room)
			->set('female',$female)
			->set('male',$male)
			->set('sex_select',$sex_select)
			->set('groups_select',$groups_select)
			->set('employees',$employees)
			->set('literacy',$literacy)
			->set('json_literacy',$json_literacy)
			->set('rooms',$rooms)
			->append_css('map.css')
			->append_css('table.css')
			->append_css('ui/jquery-ui.min.css')
			->append_css('dialog.css')
			->append_js('ui/jquery-ui.min.js')
			->append_css('chart.css')
			->append_js('chart.min.js')
			->build($this->controller.'/emp_room/'.$this->method);
	}

}